﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
namespace Guline.Web.JXAccount.Controllers
{
    public class AdministratorController : BaseController
    {
        //
        // GET: /Administrator/

        [Authorize(Users = "gulinejxad,sdatavnadm, huyhieu19911")]
        public ActionResult Index()
        {
            return View();
        }
       [Authorize(Users = "gulinejxad,sdatavnadm, huyhieu19911")]
        public ActionResult Search(string search)
        {
            if (!string.IsNullOrEmpty(search))
            {
                try
                {
                    var acc = wsjxclient.getAccountInfo(this.gulinekey, search);
                    Models.UserFullInfo accfull = new Models.UserFullInfo();
                    accfull.answer = acc.Answer;
                    accfull.ask = acc.Quest;
                    ViewBag.ask = new SelectList(this.listQuest, "ID", "Name", accfull.ask);
                    accfull.Email = acc.Email;
                    accfull.nExtPoint = acc.nExtPoint.ToString();
                    accfull.nExtPoint1 = acc.nExtPoint1.ToString();
                    accfull.nExtPoint2 = acc.nExtPoint2.ToString();
                    accfull.nExtPoint3 = acc.nExtPoint3.ToString();
                    accfull.nExtPoint4 = acc.nExtPoint4.ToString();
                    accfull.nExtPoint5 = acc.nExtPoint5.ToString();
                    accfull.nExtPoint6 = acc.nExtPoint6.ToString();
                    accfull.nExtPoint7 = acc.nExtPoint7.ToString();
                    accfull.Phone = acc.Phone;
                    accfull.Username = acc.cAccName;
                    accfull.KhoaTaiKhoan = acc.bIsBanned;
                    try
                    {
                        ViewBag.thedung = System.IO.File.ReadAllText(Server.MapPath("~/CardCharge/member/true-" + search + ".pop")).Replace("\r\n", "<br/>");
                       
                    }
                    catch
                    { 

                    }
                    try
                    {
                        ViewBag.thesai = System.IO.File.ReadAllText(Server.MapPath("~/CardCharge/member/false-" + search + ".pop")).Replace("\r\n", "<br/>");

                    }
                    catch
                    {

                    }
                   
                    return View(accfull);
                }
                catch
                {
                    ViewBag.notfound = "tài khoản không tồn tại";
                    return View();
                }
            }
            return View();
        }
        //EditInfo
        public ActionResult EditInfo(Models.UserFullInfo model)
         {
             if (ModelState.IsValid)
             {
                 if (!string.IsNullOrEmpty(model.pass1))
                 {
                     if (_useMD5)
                     {
                         model.pass1 = FormsAuthentication.HashPasswordForStoringInConfigFile(model.pass1, "MD5");
                     }
                     
                 }
                 else
                 {
                     model.pass1 = "11";
                 }
                 if (!string.IsNullOrEmpty(model.pass2))
                 {
                     if (_useMD5)
                     {
                         model.pass2 = FormsAuthentication.HashPasswordForStoringInConfigFile(model.pass2, "MD5");
                     }

                 }
                 else {
                     model.pass2 = "22";
                 }


                 if (wsjxclient.edit_Info(this.gulinekey, model.ask, model.answer, model.Phone, model.KhoaTaiKhoan, model.Username, model.pass1, model.pass2, model.Email, model.nExtPoint, model.nExtPoint1, model.nExtPoint2, model.nExtPoint3, model.nExtPoint4, model.nExtPoint5, model.nExtPoint6, model.nExtPoint7))
                 {
                      return RedirectToAction("Search",new {search =model.Username});
                 }
                 else
                 {
                     ModelState.AddModelError("", "Không thể chỉnh sửa thông tin tài khoản");
                 }
             }
             ViewBag.ask = new SelectList(this.listQuest, "ID", "Name", model.ask);
             return View("Search", model);
         }
       [Authorize(Users = "gulinejxad,sdatavnadm, huyhieu19911")]
        public ActionResult jsonList()
        {
            var list = wsjxclient.GetList().ToList();
            return Json(list, JsonRequestBehavior.AllowGet);
        }

    }
}
